﻿// -----------------------------------------------------------------------
// <copyright file="GameBoard.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Exc5
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public delegate void ChangeCellStatus(int x, int y, char symbol);

    public class GameBoard
    {
        public event ChangeCellStatus OnChangeCellStatus;

        private CheckerCell[,] m_gameBoardCells = null;
        private int m_sizeOfGameBoard;
        
        public void CreateNewBoard(int i_boardSize)
        {
            if (i_boardSize == 6 || i_boardSize == 8 || i_boardSize == 10)
            {
                m_sizeOfGameBoard = i_boardSize;
            }

            m_gameBoardCells = new CheckerCell[i_boardSize, i_boardSize];

            for (int i = 0; i < m_sizeOfGameBoard; i++)
            {
                for (int j = 0; j < m_sizeOfGameBoard; j++)
                {
                    m_gameBoardCells[j, i] = new CheckerCell();
                    m_gameBoardCells[j, i].CellColumn = (char)('A' + j);
                    m_gameBoardCells[j, i].CellRow = (char)('a' + i);
                }
            }
        }

        public void initializeGameBoardWithPlayersPawns(Player i_playerO, Player i_playerX)
        {
            for (int i = 0; i < i_playerO.NumOfPawns; i++)
            {
                m_gameBoardCells[i_playerO.GetPlayerPawnsCell(i).CellColumn - 'A', i_playerO.GetPlayerPawnsCell(i).CellRow - 'a'].PawnSymbol = 'O';
            }

            for (int i = 0; i < i_playerX.NumOfPawns; i++)
            {
                m_gameBoardCells[i_playerX.GetPlayerPawnsCell(i).CellColumn - 'A', i_playerX.GetPlayerPawnsCell(i).CellRow - 'a'].PawnSymbol = 'X';
            }
        }

        public void updatePlayerMoveInGameBoard(CheckersMove i_moveToUpdate)
        {
            m_gameBoardCells[(int)i_moveToUpdate.toCell.CellColumn - 'A', (int)i_moveToUpdate.toCell.CellRow - 'a'].PawnSymbol = i_moveToUpdate.MoveSymbol;
            m_gameBoardCells[(int)i_moveToUpdate.fromCell.CellColumn - 'A', (int)i_moveToUpdate.fromCell.CellRow - 'a'].PawnSymbol = ' ';
            if (OnChangeCellStatus != null)
            {
                OnChangeCellStatus.Invoke((int)i_moveToUpdate.toCell.CellColumn - 'A', (int)i_moveToUpdate.toCell.CellRow - 'a', i_moveToUpdate.MoveSymbol);
                OnChangeCellStatus.Invoke((int)i_moveToUpdate.fromCell.CellColumn - 'A', (int)i_moveToUpdate.fromCell.CellRow - 'a', ' ');
            }

            if(i_moveToUpdate.isEatingMove)
            {
                int rowIncreament;
                int columnIncreament;
                if (i_moveToUpdate.fromCell.CellRow > i_moveToUpdate.toCell.CellRow)
                {
                    rowIncreament = -1;
                }
                else
                {
                    rowIncreament = 1;
                }

                if (i_moveToUpdate.fromCell.CellColumn > i_moveToUpdate.toCell.CellColumn)
                {
                    columnIncreament = -1;
                }
                else
                {
                    columnIncreament = 1;
                }
                
                m_gameBoardCells[(int)(i_moveToUpdate.fromCell.CellColumn + columnIncreament - 'A'), (int)(i_moveToUpdate.fromCell.CellRow + rowIncreament - 'a')].PawnSymbol = ' ';
                OnChangeCellStatus.Invoke((int)(i_moveToUpdate.fromCell.CellColumn + columnIncreament - 'A'), (int)(i_moveToUpdate.fromCell.CellRow + rowIncreament - 'a'), ' ');
            }
        }

        public int Size
        {
            get
            {
                return m_sizeOfGameBoard;
            }

            set
            {
                if (value == 6 || value == 8 || value == 10)
                {
                    m_sizeOfGameBoard = value;
                }
            }
        }

        public CheckerCell GetCheckerCell(int i_column, int i_row)
        {
            return m_gameBoardCells[i_column, i_row];
        }
    }
}
